//
// Created by 紫霄 on 2020/4/17.
//

#ifndef LEETCODE_22_H
#define LEETCODE_22_H
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
struct ListNode {
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
  ListNode *getKthFromEnd(ListNode *head, int k) {
    int cnt = 0;
    ListNode *temp = head;
    while (temp->next) {
      temp = temp->next;
      ++cnt;
    }
    delete temp;
    for (int i = 0; i < (cnt + 1 - k); ++i) {
      head = head->next;
    }
    return head;
  }
};
#endif // LEETCODE_22_H
